package com.kefu.call.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.call.http.client.CtiClient;
import com.kefu.call.http.model.CtiResult;
import com.kefu.call.http.model.SyncSkillgroupParam;
import com.kefu.call.mapper.ActivityUserInfoMapper;
import com.kefu.call.mapper.CallCompanyMapper;
import com.kefu.call.mapper.CallSetUpMapper;
import com.kefu.call.model.CallCompany;
import com.kefu.call.model.CallSetUp;
import com.kefu.call.vo.CallSetUpUpdateVo;
import com.kefu.common.context.KefuContext;
import com.kefu.common.service.AbstractKefuService;
import com.kefu.common.util.Assert;
import com.kefu.common.util.JacksonUtil;
import com.kefu.sys.mapper.SysGroupMapper;
import com.kefu.sys.mapper.SysTenantMapper;
import com.kefu.sys.mapper.SysUserGroupMapper;
import com.kefu.sys.model.SysGroup;
import com.kefu.sys.model.SysTenant;
import com.kefu.sys.model.SysUserGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class CallSetUpService extends AbstractKefuService<CallSetUp> {
    @Resource
    private SysTenantMapper sysTenantMapper;
    @Autowired
    private CtiClient ctiClient;
    @Resource
    private SysGroupMapper sysGroupMapper;
    @Resource
    private CallSetUpMapper callSetUpMapper;
    @Resource
    private CallCompanyMapper callCompanyMapper;

    public CallSetUp info() {
        CallSetUp callSetUp = callSetUpMapper.selectFirst(Wrappers.lambdaQuery(CallSetUp.class)
                .eq(CallSetUp::getTenantId, KefuContext.getTid()));
        if (Objects.isNull(callSetUp)) {
            callSetUp = new CallSetUp();
            callSetUp.setId(IdUtil.simpleUUID());
            callSetUp.setAutoReady(0);
            callSetUp.setLockUser(0);
            callSetUp.setCallOut(0);
            callSetUp.setIsOpenIvrRecord(0);
            callSetUp.setTenantId(KefuContext.getTid());
            callSetUpMapper.insert(callSetUp);
            logger.info("初始化通话呼叫信息:{}", JacksonUtil.toString(callSetUp));
        }
        return callSetUp;
    }

    public CallSetUp updateCallSetUp(CallSetUpUpdateVo updateVo) {
        CallSetUp info = findById(updateVo.getId());
        Assert.isNull(info, "系统异常!");
        CallSetUp callSetUp = updateVo.convert();
        callSetUpMapper.updateById(callSetUp);
        logger.info("更新通话设置，信息修改:{}", JacksonUtil.toString(callSetUp));
        CallCompany callCompany = callCompanyMapper.selectByPrimaryKey(KefuContext.getTid());
        logger.info("更新通话设置，呼叫中心租户配置信息: {}", callCompany);
        if (Objects.isNull(callCompany)) {
            callCompany = new CallCompany();
            SysTenant sysTenant = sysTenantMapper.selectByPrimaryKey(KefuContext.getTid());
            callCompany.setFid(sysTenant.getTenantId());
            callCompany.setFName(sysTenant.getTenantName());
            if (Objects.nonNull(updateVo.getLockUser())) {
                callCompany.setFCallerrNumber(0 == callSetUp.getLockUser() ? -1 : callSetUp.getLockUserNum());
            }
            if (Objects.nonNull(updateVo.getIsOpenIvrRecord())) {
                callCompany.setFAdvanceRecord(updateVo.getIsOpenIvrRecord());
            }
            callCompanyMapper.insert(callCompany);
            logger.info("更新通话设置，添加租户配置信息: {}", callCompany);
        } else {
            if (Objects.nonNull(updateVo.getLockUser())) {
                callCompany.setFCallerrNumber(0 == callSetUp.getLockUser() ? -1 : callSetUp.getLockUserNum());
            }
            if (Objects.nonNull(updateVo.getIsOpenIvrRecord())) {
                callCompany.setFAdvanceRecord(updateVo.getIsOpenIvrRecord());
            }
            callCompanyMapper.updateById(callCompany);
            logger.info("更新通话设置，修改租户配置信息: {}", callCompany);
        }
        List<SysGroup> sysGroupList = sysGroupMapper.selectList(Wrappers.lambdaQuery(SysGroup.class)
                .eq(SysGroup::getTenantId, KefuContext.getTid())
                .eq(SysGroup::getGroupType, 1));
        logger.info("更新通话设置，同步的呼叫技能组:{}", JacksonUtil.toString(sysGroupList));
        if (CollUtil.isNotEmpty(sysGroupList)) {
            for (SysGroup sysGroup : sysGroupList) {
                SyncSkillgroupParam skill = new SyncSkillgroupParam();
                skill.setMaxwaittime("30");
                skill.setSvccode(sysGroup.getGroupId());
                skill.setSvcname(sysGroup.getGroupName());
                skill.setAction(String.valueOf(1));
                skill.setCompany(sysGroup.getTenantId());
                skill.setRemark(sysGroup.getCallinChoiceStrategy());
                CtiResult result = ctiClient.queueSync(skill);
                logger.info("更新通话设置，更新租户信息，同步{}呼叫技能组{}结果{}", "更新", skill, result);
            }
        }
        return callSetUp;
    }
}
